$(function() {
	if (isOnePage()) {
		$("#Pagination").hide();
	} else {
		$("#Pagination").show();
		$("#Pagination").pagination($("#Pagination").attr("recordCount"), {
			num_edge_entries : 2,  //两侧首尾分页条目数 
			num_display_entries : 3,  //连续分页主体部分分页条目数 
			callback : pageselectCallback,
			items_per_page : $("#Pagination").attr("pageSize")   //显示条数 
		});
	}
	$.fn.refreshPagination = function(page_index) {
		if (isOnePage()) {
			$("#Pagination").hide();
		} else {
			$("#Pagination").show();
			$("#Pagination").empty().pagination(
					$("#Pagination").attr("recordCount"), {
						num_edge_entries : 2,
						num_display_entries : 3,
						callback : pageselectCallback,
						current_page : page_index - 1,   //当前页索引  
						items_per_page : $("#Pagination").attr("pageSize")
					});
		}
	};
	// 分页
	function pageselectCallback(page_index, jq) {
		initData(page_index + 1);
	}
	;
	function isOnePage() {
		var total = $("#Pagination").attr("recordCount");
		var rp = $("#Pagination").attr("pageSize");
		if (isNaN(total)) {
			return true;
		}
		if (isNaN(rp)) {
			return true;
		}
		total = parseInt(total);
		rp = parseInt(rp);
		return total <=rp;
    }
});
function initData(page_index) {
$.ajax({
		url : $("#Pagination").attr("url"),
		type : "post",
		cache : false,
		data : {
			pageIndex : page_index,
			pageSize : $("#Pagination").attr("pageSize"),
			expId:$("#Pagination").attr("expId")
		},
		success : function(data) {
			var showId=$("#Pagination").attr("contentDivId");
			var json=eval("("+data+")");
			$("#" + $("#Pagination").attr("contentDivId")).html("");
			if(showId=="atList"){
				var len=json.art.length;
				var str='';
				if(len<=0){
					str='没有发表文章，暂不能浏览.';
					$("#" + $("#Pagination").attr("contentDivId")).append(str);
				}else{
					for(var i=0;i<json.art.length;i++){
						  str="<li><a target='_blank' title='"+json.art[i].title+"' href='../article/seeArticle?id="+json.art[i].id+"'>"+json.art[i].temptitle+"</a><span>"+json.art[i].createDate+"</span></li>";				  
						  $("#" + $("#Pagination").attr("contentDivId")).append(str);
					}
				}
			}
			if(showId=="bgList"){
				var len=json.blogs.length;
				var str='';
				if(len<=0){
					str='没有发表博客，暂不能浏览.';
					$("#" + $("#Pagination").attr("contentDivId")).append(str);
				}else{
					for(var i=0;i<json.blogs.length;i++){
						  var str="<li><a target='_blank' title='"+json.blogs[i].logTitle+"' href='../blog/getBlog?logId="+json.blogs[i].logId+"&username="+json.blogs[i].username+"'>"+json.blogs[i].temptitle+"</a><span>"+json.blogs[i].addTime+"</span></li>";				  
						  $("#" + $("#Pagination").attr("contentDivId")).append(str);
					}
				}
			}
			if(showId=="cgList"){
				var len=json.subs.length;
				var str='';
				if(len<=0){
					str='没有发表专题或专题未通过审核，暂不能浏览.';
					$("#" + $("#Pagination").attr("contentDivId")).append(str);
				}else{
					for(var i=0;i<len;i++){
						str='<div class="mtb10 course-group" ><div class="name">专题名称: <a title="'+json.subs[i].subName+'" target="_blank" href="showItem?userId='+json.subs[i].exp.userId+'&subId='+json.subs[i].subId+'">'+json.subs[i].temptitle
						        +'</a><span>  ('+json.subs[i].itemNum+')节</span></div><div class="price">发布时间：<b>'+json.subs[i].addTime+'</b></div>'
						        +'<div class="brief"><span>内容简介：</span>'+json.subs[i].subContent+'</div>'
						        +'<div class="showdirectory">查看专题目录</div><dl class="directory" style="display:none">'
						        +'<dt>专题目录（'+json.subs[i].itemNum+'）</dt>';
						 for(var j=0;j<json.subs[i].subItems.length;j++){
							 str=str+'<dd><em>0'+(j+1)+'</em><span><a title="'+json.subs[i].subItems[j].itemTitle+'" target="_blank" href="getItem?itemsId='+json.subs[i].subItems[j].itemsId+'&userId='+json.subs[i].exp.userId+'">'
							     +json.subs[i].subItems[j].itemTitle+'</a></span> <a class="playbtn" title="'+json.subs[i].subItems[j].itemTitle
							     +'"  href="javascript:void(0)"></a></dd>';
						 }
						        
						 str=str+'</dl></div>';	
						 $("#" + $("#Pagination").attr("contentDivId")).append(str);					
					}					
				}					
			}	
			$(".showdirectory").each(function(){
				$(this).click(function(){
					$(this).toggleClass("current");
					$(this).next(".directory").slideToggle("slow");
				});
			});
			$.fn.refreshPagination(page_index);
		},
		error : function(xhr) {
			alert("点击过快，正在加载，请稍后再次点击!"+xhr.statusText);
		}
	});
};
